export default {
	namespaced: true,
	state: {
		connectModeTxt: '设备直连',
		modelTxt: 'LPC100',
		scanneds: [],
		histories: []
	},
	mutations: {
		updateModel(state, payload) {
			state.connectModeTxt = payload.connectModeTxt;
			state.modelTxt = payload.modelTxt;
		},
		addScanned(state, devices) {
			if (devices && devices.length > 0) {
				devices.forEach(d => {
					// 已存在设备
					let deviceExists;
					for (let i = 0, len = state.scanneds.length; i < len; i++) {
						if (d.deviceId === state.scanneds[i].id) {
							deviceExists = state.scanneds[i];
							break;
						}
					}
					if (deviceExists) {
						Object.assign(deviceExists, {
							name: d.name || d.localName,
							rssi: d.RSSI
						});
					} else {
						state.scanneds.push({
							id: d.deviceId,
							name: d.name || d.localName,
							rssi: d.RSSI,
							status: 0
						});
					}
				});
			}
		},
		addHistory(state, device) {
			const deviceExists = state.histories.filter(d => d.id === device.id);
			if (!deviceExists || deviceExists.length <= 0) {
				state.histories.push(device);
			}
		},
		clearScanned(state) {
			state.scanneds = [];
		},
		clearHistory(state) {
			state.histories = [];
		}
	},
	getters: {
		modelInfo(state) {
			return {
				connectModeTxt: state.connectModeTxt,
				modelTxt: state.modelTxt
			}
		}
	}
}
